/*
 * @Author: zhuhaohua 250293786@qq.com
 * @Date: 2024-06-11 15:41:02
 * @LastEditors: DeluxePig 250293786@qq.com
 * @LastEditTime: 2024-07-10 23:03:33
 * @FilePath: \easy-talk\src\store\modules\user.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { getInfo } from '@/api/user'
import { signLogin, codeLogin } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    userData: {
      'id': 1,
      'users_uniqueid': 'n6mbo00lqb0d0djwg4dx5bnuc3npmgqr',
      'real_name': '', // 姓名
      'icon': '', // 头像
      'unit_name': '', // 单位名称
      'unit_logo': '', // 单位logo
      'unit_department': '', // 部门
      'unit_title': '', // 职位
      'unit_job': '', // 任职
      'intro': '', // 简介
      'create_datetime': '2024-04-07 13:49:04',
      'update_datetime': '2024-04-07 13:49:04',
      'phone_visible_switch': 0, // 手机号可见性:0=隐藏,1=可见
      'unit_level': '', // 单位级别
      'real_photo': ''// 真人照片
    }

  }
}

const state = {
  token: getToken(),
  userData: {
    'id': 1,
    'users_uniqueid': 'n6mbo00lqb0d0djwg4dx5bnuc3npmgqr',
    'real_name': '', // 姓名
    'icon': '', // 头像
    'unit_name': '', // 单位名称
    'unit_logo': '', // 单位logo
    'unit_department': '', // 部门
    'unit_title': '', // 职位
    'unit_job': '', // 任职
    'intro': '', // 简介
    'create_datetime': '2024-04-07 13:49:04',
    'update_datetime': '2024-04-07 13:49:04',
    'phone_visible_switch': 0, // 手机号可见性:0=隐藏,1=可见
    'unit_level': '', // 单位级别
    'real_photo': ''// 真人照片
  }
}

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_INFO: (state, DATA) => {
    state.userData = DATA
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { phone, code, official_openid } = userInfo
    let data = {
      phone: phone.trim(), code: code,
    }
    if (official_openid && official_openid !== '') {
      data.official_openid = official_openid
    }
    return new Promise((resolve, reject) => {
      signLogin(data).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token.access_token)
        setToken(data.token.access_token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  codeLoginFun({ commit }, userInfo) {
    const { official_openid } = userInfo
    return new Promise((resolve, reject) => {
      codeLogin({ official_openid: official_openid }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token.access_token)
        setToken(data.token.access_token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { data } = response
        if (data) {
          commit('SET_INFO', data)
          localStorage.setItem('userData', JSON.stringify(data))
          resolve(data)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    removeToken() // must remove  token  first
    // commit('RESET_STATE')
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },
  setStateToken({ commit, state }, data) {
    commit('SET_TOKEN', data)
    setToken(data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

